﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Animation
{
    public class AnimationState
    {
        public string StateName { get; set; }
        public int FrameCount { get; set; }
        public int RepeatCount { get; set; }
        public List<string> TextureNames { get; set; }

        [ContentSerializerIgnore]
        public List<Texture2D> Textures { get; set; }
        [ContentSerializerIgnore]
        public int CurrentFrame { get; set; }
        [ContentSerializerIgnore]
        public int CurrentRepeat { get; set; }
        
        private int currentTexture;
        
        public Texture2D GetNextFrame()
        {
            CurrentFrame--;
            if (CurrentFrame > 0)
            {
                return Textures[currentTexture];
            }
            else
            {
                if (currentTexture == Textures.Count - 1)
                {
                    CurrentRepeat--;
                    if (CurrentRepeat < 0 && RepeatCount != -1)
                    {
                        return Textures[currentTexture];
                    }
                    else
                    {
                        currentTexture = 0;
                        CurrentFrame = FrameCount;
                    }
                }
                else
                {
                    currentTexture++;
                    CurrentFrame = FrameCount;
                }
            }
            return Textures[currentTexture];
        }

        public void Reset()
        {
            CurrentFrame = FrameCount;
            CurrentRepeat = RepeatCount;
            currentTexture = 0;
        }

        public AnimationState(){
            Textures = new List<Texture2D>();

            CurrentFrame = FrameCount;
            CurrentRepeat = RepeatCount;
            currentTexture = 0;
        }

        /// <summary>
        /// Returns true if this animation state is non-repeating and has
        /// cycled through all of the frames.
        /// </summary>
        /// <returns></returns>
        public bool IsStateComplete()
        {
            return (CurrentFrame == 0) && (RepeatCount == 0);
        }

        public AnimationState Clone()
        {
            AnimationState s = new AnimationState();

            s.Textures = Textures;
            s.FrameCount = FrameCount;
            s.RepeatCount = RepeatCount;
            s.StateName = StateName;
            s.TextureNames = TextureNames;

            return s;
        }

        public void load(ContentManager cm)
        {
            foreach (string s in TextureNames)
            {
                Textures.Add(cm.Load<Texture2D>(s));
            }
        }
    }

    public class AnimationStateContentReader : ContentTypeReader<AnimationState>
    {
        protected override AnimationState Read(ContentReader input, AnimationState existingInstance)
        {
            AnimationState a = new AnimationState();

            a.StateName = input.ReadString();
            a.FrameCount = input.ReadInt32();
            a.RepeatCount = input.ReadInt32();
            a.TextureNames = input.ReadObject<List<string>>();

            a.load(input.ContentManager);

            return a;
        }
    }
}
